﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using API.Models;

namespace API.Controllers
{
    public class ZaakController : ApiController
    {
        private ZaakContext db = new ZaakContext();

        /// <summary>
        /// Get all zaken
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Zaak> GetZaaks()
        {
            return db.Zaaks.AsEnumerable();
        }

        /// <summary>
        /// Get zaak met een id
        /// </summary>
        /// <param name="id">id van de zaak</param>
        /// <returns></returns>
        public Zaak GetZaak(int id)
        {
            Zaak zaak = db.Zaaks.Find(id);
            if (zaak == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return zaak;
        }

        /// <summary>
        /// Get problemen op type
        /// </summary>
        /// <param name="type">naam van het type probleem</param>
        /// <returns></returns>
        public IEnumerable<Zaak> GetZaakmetType(string type)
        {
            List<Zaak> zaken = new List<Zaak>();
            var query = from k in db.Zaaks
                        where k.Case_Type == type
                        select new
                        {
                            k
                        };

            foreach (var zaakResult in query)
            {
                Zaak zaak = new Zaak();
                if (zaak == null)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
                }
                else
                {
                    zaak.Id = zaakResult.k.Id;
                    zaak.Address = zaakResult.k.Address;
                    zaak.Case_Group = zaakResult.k.Case_Group;
                    zaak.Case_Type = zaakResult.k.Case_Type;
                    zaak.Date_Case_Created = zaakResult.k.Date_Case_Created;
                    zaak.Description = zaakResult.k.Description;
                    zaak.Last_Inspection_Date = zaakResult.k.Last_Inspection_Date;
                    zaak.Last_Inspection_Result = zaakResult.k.Last_Inspection_Result;
                    zaak.Status = zaakResult.k.Status;
                    zaken.Add(zaak);
                }
            }
            return zaken.AsEnumerable<Zaak>();
        }


        // PUT api/Zaak/5
        public HttpResponseMessage PutZaak(int id, Zaak zaak)
        {
            if (ModelState.IsValid && id == zaak.Id)
            {
                db.Entry(zaak).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Zaak
        public HttpResponseMessage PostZaak(Zaak zaak)
        {
            if (ModelState.IsValid)
            {
                db.Zaaks.Add(zaak);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, zaak);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = zaak.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Zaak/5
        public HttpResponseMessage DeleteZaak(int id)
        {
            Zaak zaak = db.Zaaks.Find(id);
            if (zaak == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.Zaaks.Remove(zaak);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, zaak);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}